<?php
/**
 * User: hjd-yao
 * Date: 2019/1/11
 * Time: 15:58
 */

namespace App\Models\System;


use App\Models\BaseModel;
use Overtrue\Pinyin\Pinyin;

class AreaModel extends BaseModel
{
    protected $table = 'sys_area';

    public $timestamps = true;
    protected $dateFormat = 'U';

    /**
     * @title 中文转拼音
     * @param $data
     * @return mixed
     */
    public function convertName($data)
    {
        if(isset($data['en_name'])&&$data['en_name']){
            $data['first_letter'] = strtoupper(substr($data['en_name'],0,1));
            $data['area_acronym'] = isset($data['area_acronym'])&&$data['area_acronym']?$data['area_acronym']:$data['first_letter'];
            return $data;
        }
        $pinyin = new Pinyin();
        $pinyin_name = $pinyin->convert($data['name']);
        if(!$pinyin_name||empty($pinyin_name)){
            $data['en_name'] = $data['name'];
            $data['first_letter'] = strtoupper(substr($data['name'],0,1));
            $data['area_acronym'] =$data['first_letter'];
            return $data;
        }

        $area_acronym = '';
        foreach ($pinyin_name as $name){
            $area_acronym.=substr($name,0,1);
        }
        $data['en_name'] = studly_case(arr2str($pinyin_name,'_'));
        $data['first_letter'] = strtoupper(substr($data['en_name'],0,1));
        $data['area_acronym'] = strtoupper($area_acronym);

        return $data;
    }

    /**
     * @title 处理地区编码和电话区号
     * @param $parent_id
     * @param $data
     * @return mixed
     */
    public function filterCode($parent_id,$data)
    {
        $phone_code = self::where('id',$parent_id)->value('phone_code');
        if(!$phone_code){
            $phone_code = '86';
        }
        if(!isset($data['phone_code'])||!$data['phone_code']){
            $data['phone_code'] = $phone_code;
        }else{
            $data['phone_code'] = $phone_code.'+'.$data['phone_code'];
        }

        if(!isset($data['area_code'])||!$data['area_code']){
            if(isset($data['area_acronym'])){
                $data['area_code'] = $data['area_acronym'];
            }else{
                $num = self::where('parent_id',$parent_id)->count();
                $area_code = self::where('id',$parent_id)->value('area_code');
                $area_code = $area_code?$area_code:$data['phone_code'];
                if($num){
                    $data['area_code'] = $area_code.'-'.($num+1);
                }else{
                    $data['area_code'] = $area_code.'-1';
                }
            }
        }

        return $data;
    }

    /**
     * @title 获取下级地区id
     * @param $ids
     * @return array
     */
    public function getChildrenIds($ids)
    {
        $children_ids = [];
        $child_ids = self::whereIn('parent_id',$ids)->pluck('id');
        while ($child_ids->isNotEmpty()){
            $children_ids = $children_ids+$child_ids->toArray();
            $child_ids =  self::whereIn('parent_id',$child_ids)->pluck('id');
        }
        return $children_ids;
    }

    /**
     * @title 删除地区及其下级地区
     * @param $id
     * @return bool
     */
    public function delArea($id)
    {
        $info = self::where('id',$id)->first();
        if(!$info){
            $this->errorMsg = '删除信息不存在';
            return false;
        }
        $ids = self::getChildrenIds([$id]);
        $ids[] = $id;
        return self::whereIn('id',$ids)->delete();
    }


}